import React from 'react';
import RootNavigation from './src/RootNavigation';
import NavigationService from './src/NavigationService';
import {View, Clipboard, YellowBox} from 'react-native';
import * as WeChat from 'react-native-wechat';
import JPushModule from 'jpush-react-native';

YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated in plain JavaScript React classes.']);

import {wechatConfig} from './src/common/config';

import './src/common/storage';

import SearchPop from './src/components/searchPop';

WeChat.registerApp(wechatConfig.id);

export default class App extends React.Component {
    state = {
        searchKeywords: '',
        isShowSearchPop: false
    };
    setIsShowSearchPop = isShowSearchPop => {
        this.setState({
            isShowSearchPop
        });
    };

    componentDidMount() {
        JPushModule.notifyJSDidLoad((resultCode) => {
            console.log(resultCode);
        });
        JPushModule.addReceiveCustomMsgListener((message) => {
            console.log(message);
        });
        JPushModule.addReceiveNotificationListener((message) => {
            console.log("receive notification: " + message);
        });
        Clipboard.getString()
            .then(data => {
                if (data) {
                    this.setState({
                        searchKeywords: data,
                        isShowSearchPop: !true
                    });
                }
            });
    }

    componentWillUnmount() {
        JPushModule.removeReceiveCustomMsgListener();
        JPushModule.removeReceiveNotificationListener();
    }

    render() {
        const {isShowSearchPop, searchKeywords} = this.state;
        return (
            <View style={{flex: 1}}>
                <RootNavigation
                    ref={navigatorRef => {
                        NavigationService.setTopLevelNavigator(navigatorRef);
                    }}/>
                {
                    isShowSearchPop ?
                        <SearchPop searchKeywords={searchKeywords} setIsShowSearchPop={this.setIsShowSearchPop}/> : null
                }
            </View>
        );
    }
};

